package com.vids.controller;

import cn.hutool.core.collection.CollUtil;
import com.vids.utils.StringUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.vids.constant.Urlconstant;
import com.vids.constant.VIIDRespEnum;
import com.vids.model.entity.ImageBusiness;
import com.vids.model.entity.MotorVehicle;
import com.vids.model.param.MotorVehicleBatchParam;
import com.vids.model.param.MotorVehicleModel;
import com.vids.model.param.MotorVehicleParam;
import com.vids.model.param.SubImageBatchParam;
import com.vids.model.response.MotorVehicleBatch;
import com.vids.model.response.ResponseStatus;
import com.vids.model.vo.SubImageInfoVo;
import com.vids.service.IImageBusinessService;
import com.vids.service.IMotorVehicleService;
import com.vids.utils.BeanUtilss;
import com.vids.utils.BussinessIDGennelUtil;
import com.vids.utils.ImageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 机动车 前端控制器
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/VIID")
@RequiredArgsConstructor
public class MotorVehicleController
{

    public final static String fCode = "02";
    public final static String cCode = "02";

    public final static String type = "01";

    private String appId = "42010000005040000000";

    private final IMotorVehicleService motorVehicleService;

    private final IImageBusinessService imageBusinessService;



    /**
     * 校验参数
     *
     * @param status
     */
    private void validateParams(MotorVehicle tBigDataMotorVehicle, ResponseStatus status) {
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isEmpty(tBigDataMotorVehicle.getMotorVehicleID())) {
            sb.append("MotorVehicleID必填；");
        }
        if (tBigDataMotorVehicle.getInfoKind() == null) {
            sb.append("InfoKind必填；");
        }
        if (StringUtils.isEmpty(tBigDataMotorVehicle.getSourceID())) {
            sb.append("SourceID必填；");
        }
        if (StringUtils.isEmpty(tBigDataMotorVehicle.getStorageUrl1())) {
            sb.append("StorageUrl1必填；");
        }
        if (StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleColor())) {
            sb.append("VehicleColor必填；");
        }
        if (tBigDataMotorVehicle.getInfoKind() != null && tBigDataMotorVehicle.getInfoKind() == 1) {
            if (StringUtils.isEmpty(tBigDataMotorVehicle.getDeviceID())) {
                sb.append("自动采集时，DeviceID必填；");
            }
            if (tBigDataMotorVehicle.getLeftTopX() == null) {
                sb.append("自动采集时，LeftTopX必填；");
            }
            if (tBigDataMotorVehicle.getLeftTopY() == null) {
                sb.append("自动采集时，LeftTopY必填；");
            }
            if (tBigDataMotorVehicle.getRightBtmX() == null) {
                sb.append("自动采集时，RightBtmX必填；");
            }
            if (tBigDataMotorVehicle.getRightBtmY() == null) {
                sb.append("自动采集时，RightBtmY必填；");
            }
        }
        if (tBigDataMotorVehicle.getLeftTopX() != null && !tBigDataMotorVehicle.getLeftTopX().toString()
            .matches("^[0-9]+$")) {
            sb.append("LeftTopX字段为数字；");
        }
        if (tBigDataMotorVehicle.getLeftTopY() != null && !tBigDataMotorVehicle.getLeftTopY().toString()
            .matches("^[0-9]+$")) {
            sb.append("LeftTopY字段为数字；");
        }
        if (tBigDataMotorVehicle.getRightBtmX() != null && !tBigDataMotorVehicle.getRightBtmX().toString()
            .matches("^[0-9]+$")) {
            sb.append("RightBtmX字段为数字；");
        }
        if (tBigDataMotorVehicle.getRightBtmY() != null && !tBigDataMotorVehicle.getRightBtmY().toString()
            .matches("^[0-9]+$")) {
            sb.append("RightBtmY字段为数字；");
        }
        if (tBigDataMotorVehicle.getInfoKind() != null && tBigDataMotorVehicle.getInfoKind() == 2) {
            if (tBigDataMotorVehicle.getMarkTime() == null) {
                sb.append("人工采集时，MarkTime必填；");
            }
            if (tBigDataMotorVehicle.getAppearTime() == null) {
                sb.append("人工采集时，AppearTime必填；");
            }
            if (tBigDataMotorVehicle.getDisappearTime() == null) {
                sb.append("人工采集时，DisappearTime必填；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getMotorVehicleID())) {
            if (tBigDataMotorVehicle.getMotorVehicleID().length() != 48) {
                sb.append("MotorVehicleID字段长度不为48；");
            }
            if (!tBigDataMotorVehicle.getMotorVehicleID().matches("^[0-9]+$")) {
                sb.append("MotorVehicleID字段为纯数字；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getSourceID())) {
            if (tBigDataMotorVehicle.getSourceID().length() != 41) {
                sb.append("SourceID字段长度不为41；");
            }
            if (!tBigDataMotorVehicle.getSourceID().matches("^[0-9]+$")) {
                sb.append("SourceID字段为纯数字；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getDeviceID())) {
            if (tBigDataMotorVehicle.getDeviceID().length() != 20) {
                sb.append("DeviceID字段长度不为20；");
            }
            if (!tBigDataMotorVehicle.getDeviceID().matches("^[0-9]+$")) {
                sb.append("DeviceID字段为纯数字；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getTollgateID())) {
            if (tBigDataMotorVehicle.getTollgateID().length() != 20) {
                sb.append("TollgateID字段长度不为20；");
            }
            if (!tBigDataMotorVehicle.getTollgateID().matches("^[0-9]+$")) {
                sb.append("TollgateID字段为纯数字；");
            }
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getStorageUrl1()) && tBigDataMotorVehicle.getStorageUrl1()
            .length() > 256) {
            sb.append("StorageUrl1字段长度不能大于256；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getStorageUrl2()) && tBigDataMotorVehicle.getStorageUrl2()
            .length() > 256) {
            sb.append("StorageUrl2字段长度不能大于256；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getStorageUrl3()) && tBigDataMotorVehicle.getStorageUrl3()
            .length() > 256) {
            sb.append("StorageUrl3字段长度不能大于256；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getStorageUrl4()) && tBigDataMotorVehicle.getStorageUrl4()
            .length() > 256) {
            sb.append("StorageUrl4字段长度不能大于256；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getStorageUrl5()) && tBigDataMotorVehicle.getStorageUrl5()
            .length() > 256) {
            sb.append("StorageUrl5字段长度不能大于256；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getPlateDescribe()) && tBigDataMotorVehicle.getPlateDescribe()
            .length() > 64) {
            sb.append("PlateDescribe字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleStyles()) && tBigDataMotorVehicle.getVehicleStyles()
            .length() > 16) {
            sb.append("VehicleStyles字段长度不能大于16；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleHood()) && tBigDataMotorVehicle.getVehicleHood()
            .length() > 64) {
            sb.append("VehicleHood字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleTrunk()) && tBigDataMotorVehicle.getVehicleTrunk()
            .length() > 64) {
            sb.append("VehicleTrunk字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleWheel()) && tBigDataMotorVehicle.getVehicleWheel()
            .length() > 64) {
            sb.append("VehicleWheel字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleWindow()) && tBigDataMotorVehicle.getVehicleWindow()
            .length() > 64) {
            sb.append("VehicleWindow字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleRoof()) && tBigDataMotorVehicle.getVehicleRoof()
            .length() > 64) {
            sb.append("VehicleRoof字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleDoor()) && tBigDataMotorVehicle.getVehicleDoor()
            .length() > 64) {
            sb.append("VehicleDoor字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getSideOfVehicle()) && tBigDataMotorVehicle.getSideOfVehicle()
            .length() > 64) {
            sb.append("SideOfVehicle字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getCarOfVehicle()) && tBigDataMotorVehicle.getCarOfVehicle()
            .length() > 64) {
            sb.append("CarOfVehicle字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getRearviewMirror()) && tBigDataMotorVehicle.getRearviewMirror()
            .length() > 64) {
            sb.append("RearviewMirror字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleChassis()) && tBigDataMotorVehicle.getVehicleChassis()
            .length() > 64) {
            sb.append("VehicleChassis字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getVehicleShielding()) && tBigDataMotorVehicle.getVehicleShielding().length() > 64) {
            sb.append("VehicleShielding字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleBodyDesc()) && tBigDataMotorVehicle.getVehicleBodyDesc()
            .length() > 128) {
            sb.append("PlateDescribe字段长度不能大于128；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getDescOfFrontItem()) && tBigDataMotorVehicle.getDescOfFrontItem()
            .length() > 256) {
            sb.append("DescOfFrontItem字段长度不能大于256；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getDescOfRearItem()) && tBigDataMotorVehicle.getDescOfRearItem()
            .length() > 256) {
            sb.append("DescOfRearItem字段长度不能大于256；");
        }
        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getNameOfPassedRoad()) && tBigDataMotorVehicle.getNameOfPassedRoad().length() > 64) {
            sb.append("NameOfPassedRoad字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getPlateReliability()) && tBigDataMotorVehicle.getPlateReliability().length() > 3) {
            sb.append("PlateReliability字段长度不能大于3；");
        }
        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getPlateCharReliability()) && tBigDataMotorVehicle.getPlateCharReliability()
            .length() > 64) {
            sb.append("PlateCharReliability字段长度不能大于64；");
        }
        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getBrandReliability()) && tBigDataMotorVehicle.getBrandReliability().length() > 3) {
            sb.append("BrandReliability字段长度不能大于3；");
        }

        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getUsingPropertiesCode()) && !tBigDataMotorVehicle.getUsingPropertiesCode()
            .matches("^[A-R]|Z+$")) {
            sb.append("UsingPropertiesCode的值不合法[[A-R]|Z]；");
        }
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleClass()) && !tBigDataMotorVehicle.getVehicleClass()
            .matches("^[KHQZDMNTJGBX]\\d{2}+$")) {
            sb.append("VehicleClass的值不合法[[KHQZDMNTJGBX]\\d{2}];");
        }

        // PlateNo 15
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getPlateNo()) && tBigDataMotorVehicle.getPlateNo()
            .length() > 15) {
            sb.append("PlateNo字段长度不能大于15；");
        }
        // PlateNoAttach 15
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getPlateNoAttach()) && tBigDataMotorVehicle.getPlateNoAttach()
            .length() > 15) {
            sb.append("PlateNoAttach字段长度不能大于15；");
        }
        // DrivingStatusCode =4
        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getDrivingStatusCode()) && tBigDataMotorVehicle.getDrivingStatusCode().length() != 2) {
            sb.append("DrivingStatusCode字段长度不为2；");
        }
        // WheelPrintedPattern =2
        if (!StringUtils.isEmpty(
            tBigDataMotorVehicle.getWheelPrintedPattern()) && tBigDataMotorVehicle.getWheelPrintedPattern()
            .length() != 2) {
            sb.append("WheelPrintedPattern字段长度不为2；");
        }
        // VehicleModel 32
        if (!StringUtils.isEmpty(tBigDataMotorVehicle.getVehicleModel()) && tBigDataMotorVehicle.getVehicleModel()
            .length() > 32) {
            sb.append("VehicleModel字段长度不能大于32；");
        }
        // VehicleLength 5位整数，
        if (tBigDataMotorVehicle.getVehicleLength() != null && !(tBigDataMotorVehicle.getVehicleLength() + "").matches(
            "^[0-9]{5}$")) {
            sb.append("VehicleLength字段5位整数；");
        }
        // VehicleWidth 4位整数，
        if (tBigDataMotorVehicle.getVehicleWidth() != null && !(tBigDataMotorVehicle.getVehicleWidth() + "").matches(
            "^[0-9]{4}$")) {
            sb.append("VehicleWidth字段5位整数；");
        }
        // VehicleHeight 4位整数，
        if (tBigDataMotorVehicle.getVehicleHeight() != null && !(tBigDataMotorVehicle.getVehicleHeight() + "").matches(
            "^[0-9]{4}$")) {
            sb.append("VehicleLength字段5位整数；");
        }
        // Sunvisor 0：收起；1：放下
        if (tBigDataMotorVehicle.getSunvisor() != null && !Arrays.asList(0, 1)
            .contains(tBigDataMotorVehicle.getSunvisor())) {
            sb.append("Sunvisor的值不正确，Sunvisor 0：收起；1：放下；");
        }
        // SafetyBelt 0：未系；1：有系
        if (tBigDataMotorVehicle.getSafetyBelt() != null && !Arrays.asList(0, 1)
            .contains(tBigDataMotorVehicle.getSafetyBelt())) {
            sb.append("SafetyBelt的值不正确，SafetyBelt 0：未系；1：有系；");
        }
        // Calling 0：未打电话；1：打电话中
        if (tBigDataMotorVehicle.getCalling() != null && !Arrays.asList(0, 1)
            .contains(tBigDataMotorVehicle.getCalling())) {
            sb.append("Calling的值不正确，Calling 0：未打电话；1：打电话中；");
        }

        if (sb != null && !StringUtils.isEmpty(sb.toString())) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, sb.toString());
        }
    }


    /**
     * 批量新增
     *
     * @param param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/MotorVehicles", produces = "application/VIID+JSON")
    public Object postList(@RequestBody MotorVehicleBatchParam param) throws Exception {
        log.info("==========》新增机动车信息");
        List<ResponseStatus> resps = new ArrayList<ResponseStatus>();
        if (param == null || param.getMotorVehicleListObject() == null || param.getMotorVehicleListObject()
            .getMotorVehicleObject() == null || param.getMotorVehicleListObject().getMotorVehicleObject().size() == 0) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
            resps.add(status);
            return new ResponseEntity<Map<String, Object>>(
                BeanUtilss.objectToMap(BeanUtilss.getBatchResponseModel(resps)), HttpStatus.CREATED);
        }

        log.info("MotorVehicleBatchParam param=================>   "+param.toString());
        List<MotorVehicleModel> list = param.getMotorVehicleListObject().getMotorVehicleObject();

        if (CollUtil.isNotEmpty(list)) {

            log.info("保存机动车图片开始");
            //TODO:这里把数据接到公司对应的接口
            //TransactionReceipt transactionReceipt = carCallbackMock.getResult();   // 部署合约的回执信息
            for (MotorVehicleModel motorVehicleModel : list) {

                log.info("MotorVehicleID =="+motorVehicleModel.getMotorVehicleID());
                log.info("InfoKind =="+motorVehicleModel.getInfoKind().toString());
                log.info("SourceID =="+ motorVehicleModel.getSourceID());
                log.info("DeviceID =="+motorVehicleModel.getDeviceID());
                log.info("AppearTime =="+motorVehicleModel.getAppearTime());

                //把抓拍车辆图片写到本地

                List<SubImageInfoVo> subImageInfoObject = motorVehicleModel.getSubImageList().getSubImageInfoObject();

//                log.info("subImageInfoObject.size =="+subImageInfoObject.size());

                for (SubImageInfoVo subImageInfoVo: subImageInfoObject)
                {
                    if (subImageInfoVo.getData() != null) {
                        //原来第二个参数为motorVehicleModel.getMotorVehicleID()

                        log.info("开始保存车辆图片");
//                        ImageUtil.convertBase64ToImageAndSave(subImageInfoVo.getData(),
//                                                              motorVehicleModel.getDeviceID(),
//                                                              subImageInfoVo.getImageID(), "motor");
                        log.info("结束保存车辆图片");
                    }else {
//                        log.info("Vehicle Data is null.");
                    }
                }

            }

            log.info("保存机动车图片结束");
        }

//        commonSaveCarBatchModel(resps, list);
        return new ResponseEntity<Map<String, Object>>(BeanUtilss.objectToMap(BeanUtilss.getBatchResponseModel(resps)),
            HttpStatus.CREATED);
    }

    public void commonSaveCarBatchModel(List<ResponseStatus> resps, List<MotorVehicleModel> list) {
        for (MotorVehicleModel model : list) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());

            if (model == null) {
                setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
                resps.add(status);
                continue;
            }

            MotorVehicleBatch motorVehicle = BeanUtilss.transferObjectCommon(model, MotorVehicleBatch.class);
            if (model.getSubImageList() != null && model.getSubImageList()
                .getSubImageInfoObject() != null && model.getSubImageList().getSubImageInfoObject().size() > 0) {
                motorVehicle.setSubImageList(model.getSubImageList().getSubImageInfoObject());
            }

            try {
                if (motorVehicle != null && StringUtils.isEmpty(
                    motorVehicle.getMotorVehicleID()) && motorVehicle.getInfoKind() != null && motorVehicle.getInfoKind() == 1) {
                    motorVehicle.setMotorVehicleID(
                        BussinessIDGennelUtil.createViewLibraryID(appId, fCode, true, cCode));
                } else {
                    MotorVehicle TBigDataMotorVehicle2 =
                        motorVehicleService.findByMotorVehicleID(motorVehicle.getMotorVehicleID());
                    if (TBigDataMotorVehicle2 != null) {
                        setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数MotorVehicleID重复；");
                        resps.add(status);
                        continue;
                    }
                }

                validateParams(motorVehicle, status);
                if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                    if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                        MotorVehicle tBigDataMotorVehicle =
                            BeanUtilss.transferObjectIgnoreCase(motorVehicle, MotorVehicle.class);
                        motorVehicleService.save(tBigDataMotorVehicle);

                        // 图片和业务对于关系
                        saveImageBusiness(motorVehicle.getSubImageList(), type, motorVehicle.getMotorVehicleID());
                        status.setId(motorVehicle.getMotorVehicleID());
                        status.setStatusString("新增成功；");
                    }
                }
            } catch (Exception e) {
                setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
                log.error(" TBigDataMotorVehicleController  postList  Exception {} ", e);
            }
            resps.add(status);
        }
    }

    /**
     * 批量修改
     *
     * @param param
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @PutMapping(value = "/MotorVehicles", produces = "application/VIID+JSON")
    public Object putList(@RequestBody MotorVehicleBatchParam param) throws IOException {
        List<ResponseStatus> resps = new ArrayList<ResponseStatus>();
        if (param == null || param.getMotorVehicleListObject() == null || param.getMotorVehicleListObject()
            .getMotorVehicleObject() == null || param.getMotorVehicleListObject().getMotorVehicleObject().size() == 0) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
            resps.add(status);
            return BeanUtilss.getBatchResponseModel(resps);
        }

        List<MotorVehicleModel> list = param.getMotorVehicleListObject().getMotorVehicleObject();
        commonPutCarBatchModel(resps, list);
        return BeanUtilss.getBatchResponseModel(resps);
    }

    public void commonPutCarBatchModel(List<ResponseStatus> resps, List<MotorVehicleModel> list) {
        for (MotorVehicleModel model : list) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            if (model == null) {
                setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数为空；");
                resps.add(status);
                continue;
            }

            MotorVehicleBatch motorVehicle = BeanUtilss.transferObjectCommon(model, MotorVehicleBatch.class);
            if (model.getSubImageList() != null && model.getSubImageList()
                .getSubImageInfoObject() != null && model.getSubImageList().getSubImageInfoObject().size() > 0) {
                motorVehicle.setSubImageList(model.getSubImageList().getSubImageInfoObject());
            }

            try {
                validateParams(motorVehicle, status);
                if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                    updateCommon(motorVehicle, status, motorVehicle.getMotorVehicleID());
                }
            } catch (Exception e) {
                setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
                log.error(" TBigDataMotorVehicleController  putList  Exception {} ", e);
            }
            resps.add(status);
        }
    }

    /**
     * 批量删除
     *
     * @param IDList
     * @return
     */
    @DeleteMapping(value = "/MotorVehicles", produces = "application/VIID+JSON")
    public Object deleteList(@RequestParam("IDList") String IDList) {
        List<ResponseStatus> resps = new ArrayList<ResponseStatus>();
        if (StringUtils.isEmpty(IDList)) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数IDList为空；");
            resps.add(status);
            return BeanUtilss.getBatchResponseModel(resps);
        }

        String[] ids = IDList.split(",");
        for (String id : ids) {
            ResponseStatus status =
                new ResponseStatus(Urlconstant.URL_BIG_DATA_19, VIIDRespEnum.OK.getCode(), new Date());
            try {
                deleteCommon(status, id);
            } catch (Exception e) {
                setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
                log.error(" TBigDataMotorVehicleController  deleteList  Exception {} ", e);
            }
            resps.add(status);
        }
        return BeanUtilss.getBatchResponseModel(resps);
    }

    /**
     * 条件查询
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/MotorVehicles", produces = "application/VIID+JSON;charset=UTF-8")
    public Object getList(HttpServletRequest request) {
        Object attribute = request.getAttribute("map");
        MotorVehicleBatchParam param = null;
        if (attribute instanceof Map) {
            Map<String, Object> map = (Map<String, Object>)attribute;
            param = motorVehicleService.getMotorVehicleList(map);
        }
        if (param == null) {
            return new ResponseEntity<Map<String, Object>>(new HashMap<String, Object>(), HttpStatus.NO_CONTENT);
        }
        return param;
    }

    /**
     * 单个查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/MotorVehicles/{id}", produces = "application/VIID+JSON;charset=UTF-8")
    public Object get(@PathVariable("id") String id) {
        MotorVehicleBatch motorVehicle = new MotorVehicleBatch();
        try {
            MotorVehicle tBigDataMotorVehicle = motorVehicleService.findByMotorVehicleID(id);
            motorVehicle = BeanUtilss.transferObjectIgnoreCase(tBigDataMotorVehicle, MotorVehicle.class);
            if (tBigDataMotorVehicle != null && !StringUtils.isEmpty(tBigDataMotorVehicle.getMotorVehicleID())) {
                List<SubImageInfoVo> volist =
                    imageBusinessService.findSubImageInfoVo(tBigDataMotorVehicle.getMotorVehicleID(), type);
                motorVehicle.setSubImageList(volist);
            }
        } catch (Exception e) {
            log.error("TBigDataMotorVehicleController get Exception {}", e);
        }
        if (motorVehicle == null) {
            return new ResponseEntity<Map<String, Object>>(new HashMap<String, Object>(), HttpStatus.NO_CONTENT);
        }
        MotorVehicleParam param = new MotorVehicleParam();
        MotorVehicleModel model = BeanUtilss.transferObjectCommon(motorVehicle, MotorVehicleModel.class);
        if (motorVehicle.getSubImageList() != null && motorVehicle.getSubImageList().size() > 0) {
            SubImageBatchParam subImageList = new SubImageBatchParam();
            subImageList.setSubImageInfoObject(motorVehicle.getSubImageList());
            model.setSubImageList(subImageList);
        }
        param.setMotorVehicleObject(model);
        return param;
    }

    /**
     * 单个修改
     *
     * @param param
     * @param id
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonParseException
     */
    @PutMapping(value = "/MotorVehicles/{id}", produces = "application/VIID+JSON")
    public Object put(@RequestBody MotorVehicleParam param, @PathVariable("id") String id) throws IOException {
        ResponseStatus status = new ResponseStatus(Urlconstant.URL_BIG_DATA_20, VIIDRespEnum.OK.getCode(), new Date());
        if (param == null || param.getMotorVehicleObject() == null) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数motorVehicle无效，没有对于数据；");
            return BeanUtilss.getResponseModel(status);
        }

        if (StringUtils.isEmpty(id)) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数MotorVehicleID为空；");
            return BeanUtilss.getResponseModel(status);
        }
        MotorVehicleModel model = param.getMotorVehicleObject();
        MotorVehicleBatch motorVehicle = BeanUtilss.transferObjectCommon(model, MotorVehicleBatch.class);
        if (model.getSubImageList() != null && model.getSubImageList()
            .getSubImageInfoObject() != null && model.getSubImageList().getSubImageInfoObject().size() > 0) {
            motorVehicle.setSubImageList(model.getSubImageList().getSubImageInfoObject());
        }
        try {
            validateParams(motorVehicle, status);
            if (status.getStatusCode().equals(VIIDRespEnum.OK.getCode())) {
                updateCommon(motorVehicle, status, id);
            }
        } catch (Exception e) {
            log.error("TBigDataMotorVehicleController put Exception {}", e);
            setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
        }
        return BeanUtilss.getResponseModel(status);
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/MotorVehicles/{id}", produces = "application/VIID+JSON")
    public Object delete(@PathVariable("id") String id) {
        ResponseStatus status = new ResponseStatus(Urlconstant.URL_BIG_DATA_20, VIIDRespEnum.OK.getCode(), new Date());
        if (StringUtils.isEmpty(id)) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数MotorVehicleID为空；");
            return BeanUtilss.getResponseModel(status);
        }
        try {
            deleteCommon(status, id);
        } catch (Exception e) {
            log.error("TBigDataMotorVehicleController delete Exception {}", e);
            setStatus(status, VIIDRespEnum.DEVICE_ERROR, "请联系管理员；");
        }
        return BeanUtilss.getResponseModel(status);
    }

    public void deleteCommon(ResponseStatus status, String id) throws Exception {
        MotorVehicle TBigDataMotorVehicle2 = motorVehicleService.findByMotorVehicleID(id);
        if (TBigDataMotorVehicle2 == null) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数MotorVehicleID无效，没有对于数据；");
        } else {
            motorVehicleService.deleteByMotorVehicleID(id);
            status.setId(id);
            status.setStatusString("删除成功；");
        }
    }

    public void updateCommon(MotorVehicleBatch motorVehicle, ResponseStatus status, String id) throws Exception {
        MotorVehicle TBigDataMotorVehicle2 = motorVehicleService.findByMotorVehicleID(id);
        if (TBigDataMotorVehicle2 == null) {
            setStatus(status, VIIDRespEnum.INVALID_JSON_CONTENT, "参数MotorVehicleID无效，没有对于数据；");
        } else {
            motorVehicle.setId(TBigDataMotorVehicle2.getId());
            MotorVehicle tBigDataMotorVehicle = BeanUtilss.transferObjectIgnoreCase(motorVehicle, MotorVehicle.class);
            motorVehicleService.updateById(tBigDataMotorVehicle);

            // 图片和业务对于关系
            saveImageBusiness(motorVehicle.getSubImageList(), type, motorVehicle.getMotorVehicleID());
            status.setId(motorVehicle.getMotorVehicleID());
            status.setStatusString("修改成功；");
        }
    }

    /**
     * 设置返回值代码和消息 VIID 标准
     *
     * @param res
     * @param e
     * @param field
     */
    protected void setStatus(ResponseStatus res, VIIDRespEnum e, String field) {
        StringBuffer sb = new StringBuffer();
        sb.append(e.getMsg() + "[" + field + "]");
        res.setStatusCode(e.getCode());
        res.setStatusString(sb.toString());
    }

    public void saveImageBusiness(List<SubImageInfoVo> subImageList, String type, String ywId) {
        if (subImageList != null && !subImageList.isEmpty()) {
            for (SubImageInfoVo vo : subImageList) {
                if (vo == null || StringUtils.isEmpty(vo.getImageID())) {
                    continue;
                }
                ImageBusiness tBigDataImageBusiness = new ImageBusiness();
                tBigDataImageBusiness.setImageId(vo.getImageID());
                tBigDataImageBusiness.setYwId(ywId);
                tBigDataImageBusiness.setBigType(type);
                tBigDataImageBusiness.setImageType(type);

                if ("01".equals(type)) {
                    if (!StringUtils.isEmpty(vo.getType()) && Arrays.asList("01", "02", "03", "04", "05", "06", "07",
                        "08", "09").contains(vo.getType())) {
                        tBigDataImageBusiness.setImageType(vo.getType());
                    }
                }
                // 先删除
                deleteImageBusiness(type, ywId);
                // 新增
                imageBusinessService.save(tBigDataImageBusiness);
            }
        }
    }

    public void deleteImageBusiness(String type, String ywId) {
        ImageBusiness tBigDataImageBusiness = new ImageBusiness();
        tBigDataImageBusiness.setYwId(ywId);
        tBigDataImageBusiness.setBigType(type);
        imageBusinessService.deleteImageBusiness(tBigDataImageBusiness);
    }

}
